﻿using SmallTool.BaseFile;
using SmallTool.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SmallTool.frm
{
    public partial class frm_sqlList : Form
    {
        public frm_sqlList()
        {
            InitializeComponent();
        }
        private SelectSQLModel model = null;
        public string SelectSQL { get; set; }
        private void frm_sqlList_Load(object sender, EventArgs e)
        {
            dgv_sql.DataSource = BaseCS.instance.GetSelectSql();
        }

        private void btn_clear_Click(object sender, EventArgs e)
        {
            Clear();
        }

        private void Clear()
        {
            txt_ID.Text = string.Empty;
            txt_sql.Text = string.Empty;
            txt_sqlName.Text = string.Empty;
            btn_addOrUpdate.Text = "添加";
            txt_ID.Enabled = true;
        }

        private void dgv_sql_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            // 获取选中行信息
            if (e.RowIndex < 0 || e.ColumnIndex < 0) return;
            if (e.ColumnIndex == 0)
            {
                if ((bool)dgv_sql.Rows[e.RowIndex].Cells[e.ColumnIndex].FormattedValue)
                {
                    dgv_sql.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = false;
                }
                else
                {
                    dgv_sql.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = true;
                }
            }
            else
            {
                string id = dgv_sql.Rows[e.RowIndex].Cells[1].Value.ToString().Trim();
                model = BaseCS.instance.GetModelSelectSQL(id);
                if (model != null)
                {
                    txt_ID.Enabled = false;
                    txt_ID.Text = model.ID;
                    txt_sql.Text = model.SelectSQL;
                    txt_sqlName.Text = model.SQLName;
                    btn_addOrUpdate.Text = "修改";
                }
            }
        }

        private void btn_addOrUpdate_Click(object sender, EventArgs e)
        {
            string _id = txt_ID.Text.Trim();
            string _sql = txt_sql.Text.Trim();
            string _sqlName = txt_sqlName.Text.Trim();
            if (string.IsNullOrWhiteSpace(_id) || string.IsNullOrWhiteSpace(_sql) || string.IsNullOrWhiteSpace(_sqlName))
            {
                MessageBox.Show("请输入完整的信息");
                return;
            }

            SelectSQLModel model = new SelectSQLModel(_id, _sql, _sqlName);
            string btnText = btn_addOrUpdate.Text.Trim();
            if (btnText.Equals("添加"))
            {
                if (BaseCS.instance.IsExistsID(_id))
                {
                    MessageBox.Show("相同的标识已经存在");
                    return;
                }
                BaseCS.instance.AddSelectSQL(model);
                MessageBox.Show("添加成功");
            }
            else if (btnText.Equals("修改"))
            {
                BaseCS.instance.UpdateSelectSQL(model);
                MessageBox.Show("修改成功");
            }
            Clear();
            dgv_sql.DataSource = new List<SelectSQLModel>();
            dgv_sql.DataSource = BaseCS.instance.GetSelectSql();
        }

        private void btn_del_Click(object sender, EventArgs e)
        {
            string _id = txt_ID.Text.Trim();
            if (string.IsNullOrWhiteSpace(_id))
            {
                MessageBox.Show("请选择要删除的语句");
                return;
            }
            BaseCS.instance.DeleteSelectSQL(_id);
            Clear();
            dgv_sql.DataSource = new List<SelectSQLModel>();
            dgv_sql.DataSource = BaseCS.instance.GetSelectSql();
            MessageBox.Show("删除成功");
        }

        private void btn_select_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < dgv_sql.Rows.Count; i++)
            {
                if ((bool)dgv_sql.Rows[i].Cells[0].FormattedValue)
                {
                    if (!string.IsNullOrWhiteSpace(sb.ToString())) { sb.Append(","); }
                    sb.Append(dgv_sql.Rows[i].Cells[1].Value.ToString());
                }
            }
            if (string.IsNullOrWhiteSpace(sb.ToString()))
            {
                MessageBox.Show("您没有选中一个语句");
                return;
            }
            this.SelectSQL = sb.ToString();
            this.Close();
        }
    }
}
